package org.alex.samples.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.alex.samples.dao.BaseHibernateDAO;
import org.alex.samples.dao.ITestDao;
import org.alex.samples.model.ExamTest;
import org.alex.samples.model.ExamTestVO;
import org.alex.samples.model.PagerModel;
import org.alex.samples.model.SystemContext;
import org.alex.samples.model.User;
import org.alex.samples.model.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class ITestDaoImpl extends BaseHibernateDAO<ExamTest> implements ITestDao {

	@Override
	public Iterator<ExamTest> iterate(String queryString, Object[] values) {
		return null;
	}

	@Override
	public List<ExamTest> findAllTests() throws Exception {
		return loadAll();
	}

	@Override
	public void createTest(ExamTest examTest) throws Exception {
		save(examTest);
	}

	@Override
	public ExamTest getTestById(Integer id) throws Exception {
		return get(id);
	}

	@Override
	public void editTest(ExamTest examTest) throws Exception {
		update(examTest);
	}

	@Override
	public void deletetTest(Integer id) throws Exception {
		deleteByKey(id);
	}

	@Override
	public List<ExamTest> findAllTestType() throws Exception {
		String hql = "FROM ExamTest et,ExamTestType ett";
		return find(hql);
	}

	@SuppressWarnings("unchecked")
	@Override
	public PagerModel<ExamTestVO> findAllByPage() {
		PagerModel<ExamTestVO> page = new PagerModel<ExamTestVO>();
		ArrayList<ExamTestVO> datas = new ArrayList<ExamTestVO>();
		int offset = SystemContext.getOffset();
		int pageSize = SystemContext.getSize();

		// 使用hibernate完成分页
		List<ExamTest> tempDates = this.getSession().createQuery("from ExamTest").setFirstResult(offset).setMaxResults(pageSize)
				.list();
		for (ExamTest examTest : tempDates) {
			ExamTestVO examTestVO = new ExamTestVO();
			BeanUtils.copyProperties(examTest, examTestVO);
			datas.add(examTestVO);
		}
		page.setDatas(datas);
		page.setOffset(offset); 
		page.setPageSize(pageSize);

		// 获取总记录数
		long totalSize = (long) this.getSession().createQuery("select count(*) from ExamTest").uniqueResult();
		page.setTotalSize(totalSize);
		return page;
	}
	@SuppressWarnings("unchecked")
	@Override
	public PagerModel<ExamTestVO> findSubTestByPage(Integer id) {
		PagerModel<ExamTestVO> page = new PagerModel<ExamTestVO>();
		ArrayList<ExamTestVO> datas = new ArrayList<ExamTestVO>();
		int offset = SystemContext.getOffset();
		int pageSize = SystemContext.getSize();

		// 使用hibernate完成分页
		List<ExamTest> tempDates = this.getSession().createQuery("from ExamTest where subID = "+id).setFirstResult(offset).setMaxResults(pageSize)
				.list();
		for (ExamTest examTest : tempDates) {
			ExamTestVO examTestVO = new ExamTestVO();
			BeanUtils.copyProperties(examTest, examTestVO);
			datas.add(examTestVO);
		}
		page.setDatas(datas);
		page.setOffset(offset); 
		page.setPageSize(pageSize);

		// 获取总记录数
		long totalSize = (long) this.getSession().createQuery("select count(*) from ExamTest where subID = "+id).uniqueResult();
		page.setTotalSize(totalSize);
		return page;
	}

	@Override
	public List<ExamTest> getTestListBySubAndTestType(Integer subId, Integer id) throws Exception {
		String sql="from ExamTest where subID="+subId+" and testTypeId='"+id+"'";
		return find(sql);
	}

	@Override
	public List<ExamTest> getTestListByIdList(List<Integer> randomTestIdList) {
		String sql="from ExamTest where id in("+randomTestIdList.get(0);
		for(Integer id:randomTestIdList){
			sql+=","+id;
		}
		sql+=")";
		return find(sql);
	}
	
	

}